package com.presto.directo.mvvm.view

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.widget.doOnTextChanged
import com.presto.directo.base.BaseActivity
import com.presto.directo.databinding.ActivityAddressinfoBinding
import com.presto.directo.mvvm.viewmodel.AddressViewModel
import com.presto.directo.mvvm.viewmodel.getViewModel
import com.presto.directo.net.getHttpMap
import com.presto.directo.utils.*
import ru.whalemare.sheetmenu.SheetMenu

class InfoAddressActivity : BaseActivity() {
    override val rootView: View
        get() = binding.root

    private val binding by lazy { ActivityAddressinfoBinding.inflate(layoutInflater) }
    private val views by lazy {
        arrayListOf(
            binding.etPostCode,
            binding.etRegion1,
            binding.etRegion2,
            binding.etRegion3,
            binding.etAddress,
            binding.etInner
        )
    }
    private val viewModel: AddressViewModel by lazy { getViewModel() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Af.show_address_page.track(this)
        viewModel.infoLiveData.observe(this) {
            binding.etPostCode.setText(it.post_code)
            binding.etRegion1.setText(it.region_1_name)
            binding.etRegion2.setText(it.region_2_name)
            binding.etRegion3.setText(it.region_3_name)
            binding.etAddress.setText(it.address)
            binding.etInner.setText(it.inner_number)
            binding.etOuter.setText(it.out_number)
        }
        viewModel.cityUpdateLiveData.observe(this) {
            refresh(false)
        }
        viewModel.uploadLiveData.observe(this) {
            Af.success_submit_addressInfo.track(this)
            startActivity(Intent(this, InfoWorkActivity::class.java))
            finish()
        }
        viewModel.getInfo()
    }

    override fun initEvent() {
        views.forEach {
            it.doOnTextChanged { text, start, before, count ->
                if (it == binding.etPostCode) {
                    viewModel.postCode = binding.etPostCode.text.toString()
                    if (binding.etPostCode.text.length > 3) {
                        refresh(true)
                    } else {
                        binding.etRegion1.setText("")
                        binding.etRegion2.setText("")
                        binding.etRegion3.setText("")
                    }
                }
                if (it == binding.etRegion1) {
                    if (viewModel.regionMap[binding.etPostCode.text.toString()].isNullOrEmpty()) {
                        viewModel.regionId1 = ""
                        viewModel.regionId2 = ""
                        viewModel.regionId3 = ""
                        binding.etRegion3.isFocusableInTouchMode = true
                    } else {
                        binding.etRegion3.isFocusableInTouchMode = false
                    }
                }
                updateBtn()
            }
        }
        binding.etRegion3.setOnClickListener {
            viewModel.regionMap[binding.etPostCode.text.toString()]?.let { list ->
                if (list.isNotEmpty()) {
                    SheetMenu(
                        actions = list.map { it.asentamiento },
                        onClick = {
                            binding.etRegion3.setText(it.title)
                            viewModel.regionId3 = list[it.id].id
                        },
                        layoutProvider = MyLayoutProvider()
                    ).show(this)
                }
            }
        }
        binding.btnNext.setOnClickListener {
            Af.click_submit_addressInfo.track(this)
            val map = getHttpMap()
            map["post_code"] = binding.etPostCode.text.toString()
            map["region_1"] = viewModel.regionId1
            map["region_1_name"] = binding.etRegion1.text.toString()
            map["region_2"] = viewModel.regionId2
            map["region_2_name"] = binding.etRegion2.text.toString()
            map["region_3"] = viewModel.regionId3
            map["region_3_name"] = binding.etRegion3.text.toString()
            map["address"] = binding.etAddress.text.toString()
            map["out_number"] = binding.etOuter.text.toString()
            map["inner_number"] = binding.etInner.text.toString()
            viewModel.upload(map)
        }
    }

    private fun refresh(fromUser: Boolean) {
        val regions = viewModel.regionMap[binding.etPostCode.text.toString()]
        if (!regions.isNullOrEmpty()) {
            viewModel.regionId1 = regions[0].idEstado
            viewModel.regionId2 = regions[0].idMunicipio
            binding.etRegion1.setText(regions[0].estado)
            binding.etRegion2.setText(regions[0].municipio)
        } else {
            binding.etRegion1.setText("")
            binding.etRegion2.setText("")
            binding.etRegion3.setText("")
            if (fromUser) {
                viewModel.getCity(viewModel.postCode)
            }
        }
    }

    override fun updateBtn() {
        binding.btnNext.isEnabled = views.all { it.text.isNotBlank() }
    }

}